package Application.Utils;

import Application.Interface.FileOutputListener;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;

import static Application.Utils.StaticUtil.config;

public class FileOutputUtil {
    protected FileOutputUtil(){}

    public static void Output(JFrame frame, String fileName, FileOutputListener action)
    {
        JFileChooser chooser = StaticUtil.chooser;
        chooser.setFont(config.fontGetter());
        chooser.setDialogTitle("保存至");
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.setSelectedFile(new File(fileName + ".xml"));
        chooser.setCurrentDirectory(config.default_open_path);
        chooser.addChoosableFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                } else return f.getName().endsWith(".xml");
            }

            @Override
            public String getDescription() {
                return "XAML源文件(.xml)";
            }
        });
        int result = chooser.showOpenDialog(frame);
        if (result == JFileChooser.APPROVE_OPTION)
        {
            action.action();
        }
        else
        {
            StaticUtil.errorReport.append("AI:已取消文件输出！\n");
        }
    }

    public static void SetSavePath(JFrame frame)
    {
        JFileChooser path_chooser = new JFileChooser();
        path_chooser.setFont(config.fontGetter());
        path_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        path_chooser.setDialogTitle("选择");
        path_chooser.setAcceptAllFileFilterUsed(false);
        path_chooser.addChoosableFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                return f.isDirectory();
            }

            @Override
            public String getDescription() {
                return "目录";
            }
        });
        path_chooser.showOpenDialog(frame);
        config.default_open_path = path_chooser.getSelectedFile();
    }
    public static void SetSavePath() {SetSavePath(StaticUtil.summonerFrame);}

}

